package com.yurun.loginmodule.controller;

import com.yurun.commonapi.api.CommonResult;
import com.yurun.commonapi.pojo.StudentDocument;
import com.yurun.commonapi.vo.StudentSignListVO;
import com.yurun.loginmodule.util.JWTUtil;
import com.yurun.orgstudentmodule.service.StudentInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.web.util.WebUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletRequest;
import java.math.BigInteger;
import java.util.Date;

@Api(tags = "学生模块")
@RestController
@RequestMapping("/studentManage")
public class StudentInfoController {

    @Autowired
    private StudentInfoService studentInfoService;

    @ApiOperation(value = "根据条件查询学生信息")
    //@RequiresPermissions("/allStudentInfo")
    @RequestMapping("/allStudentInfo")
    public CommonResult allStudentInfo(ServletRequest request, BigInteger branchId, String keyword, int isImport, int pageNum, int pageSize) {
        // 获取token
        String token = WebUtils.toHttp(request).getHeader("Authorization").substring(6);
        // 获取机构id
        String organizationId = JWTUtil.getOrganizationId(token);
        // 获取员工id
        String employeeId = JWTUtil.getUsername(token);
        return studentInfoService.allStudentInfo(isImport,BigInteger.valueOf( Long.parseLong(organizationId)), BigInteger.valueOf(Long.parseLong(employeeId)), branchId, keyword, pageNum, pageSize);
    }

    @ApiOperation(value = "新增学生")
    //@RequiresPermissions("/addStudent")
    @RequestMapping("/addStudent")
    public CommonResult addStudent(ServletRequest request, StudentDocument studentDocument) {
        // 获取token
        String token = WebUtils.toHttp(request).getHeader("Authorization").substring(6);
        // 获取机构id
        String organizationId = JWTUtil.getOrganizationId(token);
        studentDocument.setOrganizationId(BigInteger.valueOf(Long.parseLong(organizationId)));
        return studentInfoService.addStudent(studentDocument);
    }

    @ApiOperation(value = "获取某一个学生信息")
    //@RequiresPermissions("/getOneStudentInfo")
    @RequestMapping("/getOneStudentInfo")
    public CommonResult getOneStudentInfoByStudentId(ServletRequest request, StudentDocument studentDocument) {
        // 获取token
        String token = WebUtils.toHttp(request).getHeader("Authorization").substring(6);
        // 获取机构id
        String organizationId = JWTUtil.getOrganizationId(token);
        studentDocument.setOrganizationId(BigInteger.valueOf(Long.parseLong(organizationId)));
        return studentInfoService.getOneStudentInfoByStudentId(studentDocument);
    }

    @ApiOperation(value = "修改学生信息")
    //@RequiresPermissions("/editStudent")
    @RequestMapping("/editStudent")
    public CommonResult editStudent(ServletRequest request, StudentDocument studentDocument) {
        // 获取token
        String token = WebUtils.toHttp(request).getHeader("Authorization").substring(6);
        // 获取机构id
        String organizationId = JWTUtil.getOrganizationId(token);
        // 获取员工id
        String employeeId = JWTUtil.getUsername(token);
        studentDocument.setOrganizationId(BigInteger.valueOf(Long.parseLong(organizationId)));
        studentDocument.setUpdateTime(new Date());
        return studentInfoService.editStudent(BigInteger.valueOf(Long.parseLong(employeeId)), studentDocument, 3);
    }

    @ApiOperation(value = "删除学生")
    //@RequiresPermissions("/delStudent")
    @RequestMapping("/delStudent")
    public CommonResult delStudent(ServletRequest request, StudentDocument studentDocument) {
        // 获取token
        String token = WebUtils.toHttp(request).getHeader("Authorization").substring(6);
        // 获取机构id
        String organizationId = JWTUtil.getOrganizationId(token);
        // 获取分店id
        String branchId = JWTUtil.getBranchId(token);
        // 获取员工id
        String employeeId = JWTUtil.getUsername(token);
        studentDocument.setBranchId(BigInteger.valueOf(Long.parseLong(branchId)));
        studentDocument.setOrganizationId(BigInteger.valueOf(Long.parseLong(organizationId)));
        studentDocument.setUpdateTime(new Date());
        studentDocument.setIsDel(1);
        return studentInfoService.delStudent(BigInteger.valueOf(Long.parseLong(employeeId)),studentDocument, 3);
    }

    @ResponseBody
    @PostMapping("/getStudentSignList")
    public CommonResult getStudentSignList(@RequestBody StudentSignListVO  studentSignListVO, ServletRequest request) {
        // 获取token
        String token = WebUtils.toHttp(request).getHeader("Authorization").substring(6);
        // 获取机构id
        String organizationId = JWTUtil.getOrganizationId(token);
        studentSignListVO.setOrganizationId(BigInteger.valueOf(Long.parseLong(organizationId)));
        return studentInfoService.getStudentSignList(studentSignListVO);
    }
}
